import { message } from 'ant-design-vue'
import { ref } from 'vue'

import { accountApi } from '@/apis'
import { AccountItem } from '@/apis/accountTypes'

type Params = {
  refresh: PromiseFunc
}

export function useChangePassword({ refresh }: Params) {
  const selectedItem = ref({} as AccountItem)
  const showChange = ref(false)
  const newPassword = ref('')

  function handleShowChange(item: AccountItem) {
    newPassword.value = ''
    selectedItem.value = item
    showChange.value = true
  }

  async function handleChangePassword() {
    await refresh(async () => {
      showChange.value = false
      try {
        await accountApi.resetPassword({
          id: selectedItem.value.id,
          password: newPassword.value
        })
        message.success('修改密码成功')
      } finally {
      }
    })
  }

  return {
    showChange,
    newPassword,
    handleShowChange,
    handleChangePassword
  }
}
